package dataBase.magaers;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import dataBase.ConnectionManager;
import dataBase.DBInitializer;
import dataBase.DBNotInitializedException;
import dataBase.ISqlTable;
import dataBase.User;
import dataBase.UserPasswordSqlTable;

import servlets.ErrorCode;

public class UserDBMngr {
	private static UserDBMngr instance;

	ConnectionManager connectionManager = null;
	DBInitializer initializer = null;
	ISqlTable userPasswordTable = null;
	ISqlTable userInfoTable = null;
	
	User currentUser = null;
		
	private UserDBMngr() throws DBNotInitializedException, ClassNotFoundException{
		
		connectionManager = ConnectionManager.getInstance();
		initializer = DBInitializer.getInstance();

		userPasswordTable = initializer.getUserPasswordTable();
		userInfoTable = initializer.getUserInfoTable();
	}
	
	public static UserDBMngr getInstance(){
		if (instance == null){
			try {
				instance = new UserDBMngr();
			} catch (DBNotInitializedException e) {
				return null;			
			} catch (ClassNotFoundException e) {
			}
		}
		return instance ;
	}
	
	public ErrorCode addNewUser(User toAdd) {
		Statement stmt = connectionManager.getStatement(Connection.TRANSACTION_SERIALIZABLE);
		if (stmt == null){
			return ErrorCode.CANNOT_CONNECT_TO_DB;
		}
		try {
			userInfoTable.insertRecord(stmt, toAdd.getUserInfo());
			userPasswordTable.insertRecord(stmt, toAdd.getUserPassword());
		} catch (SQLException e) {
			connectionManager.rollbackAndClose(stmt);
			return ErrorCode.USER_CREATE_ERROR;			
		}
		if (!connectionManager.commitAndClose(stmt)){
			return ErrorCode.USER_CREATE_ERROR;
		}
		return ErrorCode.NO_ERROR;
	}

	public boolean validatePassword(String username, String password){
		Statement stmt = connectionManager.getStatement(Connection.TRANSACTION_READ_COMMITTED);
		if (stmt == null){
			return false;
		}
		try{
			if (((UserPasswordSqlTable) userPasswordTable).validatePassword(stmt, username, password)){
				if (!connectionManager.commitAndClose(stmt)){
					return false;
				}
				return true;
			}
		}
		catch (SQLException exception){
			connectionManager.rollbackAndClose(stmt);
			return false;
		}
		return false;
	}

	public ISqlTable getUserInfoTable() {
		return userInfoTable;
	}
	
	public boolean isAdmin(String uid){
		if ((uid == null) || (uid.equals(""))) {
			return false;
		}
		Statement stmt = connectionManager.getStatement(Connection.TRANSACTION_READ_COMMITTED);
		if (stmt == null){
			return false;
		}
		String query = "SELECT admin FROM " + userInfoTable.getName() + " WHERE uid='" + uid + "' AND admin='true';";
		try {
			ResultSet rs = stmt.executeQuery(query);
			boolean res = rs.next();
			connectionManager.commitAndClose(stmt);
			return res;
		} catch (SQLException e) {
			connectionManager.rollbackAndClose(stmt);
			return false;
		}
	}
}
